package com.zoo.boot.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zoo.boot.api.CommonResult;
import com.zoo.boot.api.PagingDto;
import com.zoo.boot.model.LDepartments;
import com.zoo.boot.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: WangB
 * @Date: 2021/6/23
 */
@Controller
@Api(tags = "CyDepartmentController", description = "部门管理")
@RequestMapping("/cy/dept")
public class CyDepartmentController {


    @Autowired
    private DepartmentService departmentService;


    @ApiOperation("展示部门信息")
    @GetMapping("/index")
    public String index() {
        return "company/dept/dept";
    }

    @ApiOperation("展示部门列表")
    @GetMapping("/list")
    @ResponseBody
    public CommonResult deptList(LDepartments dept, PagingDto dto) {
        Page<Object> objects = PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<LDepartments> list = departmentService.list(dept);
        return CommonResult.tableSuccess(list).setCount(objects.getTotal());
    }

    @ApiOperation("展示添加页面")
    @GetMapping("/add")
    public String navAddPage() {
        return "company/dept/dept-add";
    }


    // 说明一下，这里的部门号需要去数据库中识别，如果以存在就不保存，不存在则保存（自行实现）
    @ApiOperation("添加部门信息")
    @PostMapping
    @ResponseBody
    public CommonResult add(@RequestBody LDepartments departments) {
        if (departmentService.add(departments)) {
            return CommonResult.success();
        }
        return CommonResult.fail();
    }

    @ApiOperation("展示编辑页面")
    @GetMapping("/edit")
    public  String  navUpdatePage(String  deptCode, Map model){
        LDepartments deptByCode = departmentService.getDeptByCode(deptCode);
        model.put("dept",deptByCode);
        return  "company/dept/dept-edit";
    }


    @ApiOperation("修改部门信息<仅仅修改部门名称>")
    @PutMapping
    @ResponseBody
    public  CommonResult  update(@RequestBody LDepartments departments){
        if(departmentService.updateDeptByCode(departments)){
          return   CommonResult.success();
        }
        return  CommonResult.fail();
    }


    @ApiOperation("根据部门编号删除部门记录")
    @DeleteMapping
    @ResponseBody
    public CommonResult  delete(String  deptCode){
        if(departmentService.deleteDeptById(deptCode)){
            return  CommonResult.success();
        }
        return  CommonResult.fail();
    }

    @ApiOperation("批量删除记录")
    @DeleteMapping("/batch")
    @ResponseBody
    public CommonResult  batchDel(@RequestBody List<String>  ids){
        if(departmentService.batchDelete(ids)){
            return  CommonResult.success();
        }
        return CommonResult.fail();
    }
}
